package com.classeven.downhill.state;

import org.andengine.engine.options.ScreenOrientation;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.KeyEvent;

import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class Main extends GameEngine implements iGameDefines{


	public State_GamePlay play;
	public State_Logo logo;
	State_MainMenu menu;
	State_GamePause pause;
	State_Loading loading;
	State_Select_Arena arena;
	
	SensorManager sensorManager;
	Sensor accelerometer;
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		mClickDetector.onTouchEvent(pSceneTouchEvent);
//		mScrollDetector.onTouchEvent(pSceneTouchEvent);
		
		switch (gameCurrentState)
		{
			case GAME_STATE_SELECT_ARENA:
				arena.sceneTouch(pScene, pSceneTouchEvent);
		
			case GAME_STATE_GAME_PLAY:
				play.sceneTouch(pScene, pSceneTouchEvent);
				break;
		}
		
		
		return false;
	}

	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY) {
		// TODO Auto-generated method stub
		switch (gameCurrentState)
		{
			case GAME_STATE_SELECT_ARENA:
			arena.click(pClickDetector, pPointerID, pSceneX, pSceneY);
				break;
		}
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		
		switch (gameCurrentState)
		{
			
			case GAME_STATE_MAIN_MENU:
				menu.event(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			break;
			
			case GAME_STATE_SELECT_ARENA:
				arena.event(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			break;
			
			case GAME_STATE_GAME_PLAY:
				play.event(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			break;
			
			case GAME_STATE_PAUSE:
				pause.event(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
				break;
		}
		
		return false;
	}

	@Override
	public void onScrollStarted(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScroll(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) {
		// TODO Auto-generated method stub
		
	}


	public void GameInit()
	{
		SCREEN_WIDTH = this.getWindowManager().getDefaultDisplay().getWidth();
		SCREEN_HEIGHT = this.getWindowManager().getDefaultDisplay().getHeight();
		
		ORIENTATION = ScreenOrientation.PORTRAIT_FIXED;
		
		if(ORIENTATION == ScreenOrientation.PORTRAIT_FIXED || ORIENTATION == ScreenOrientation.PORTRAIT_SENSOR)
		{
			CAMERA_WIDTH = SCREEN_WIDTH;
			CAMERA_HEIGHT = SCREEN_HEIGHT;
		}
		else if(ORIENTATION == ScreenOrientation.LANDSCAPE_FIXED || ORIENTATION == ScreenOrientation.LANDSCAPE_SENSOR)
		{
			CAMERA_WIDTH = SCREEN_WIDTH;
			CAMERA_HEIGHT = SCREEN_HEIGHT;
		}
		
		
	}

	public void GameUpdate()
	{
		gameMechine();
		gameSetUpdate();
		
//		Utils.TRACE("state massage "+messageCurrentState);
//		Utils.TRACE("state  "+gameCurrentState);
//		
	}

	
	void gameSetUpdate() 
	{
		gameCurrentState = gameNextState;
		messageCurrentState = messageNextState;
	}
	
	private void gameMechine() {
		
		switch (gameCurrentState) {
		case GAME_STATE_INIT:
			state_Init();
			break;

		case GAME_STATE_LOGO:
			
			state_Logo();
			break;
		
		case GAME_STATE_LOADING:
			
			state_Loading();
			break;
		
		case GAME_STATE_MAIN_MENU:
			
			state_Menu();
			break;
		
		case GAME_STATE_SELECT_ARENA:
			
			state_Arena();
			break;
		
		case GAME_STATE_GAME_PLAY:
			state_GamePlay();
			break;
		
		case GAME_STATE_PAUSE:
			state_Pause();
			break;
		}
		
	}
	


	private void state_Init() {
		
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			logo = new State_Logo(this);
			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE) 
		{
			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT) 
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
	}
	
	private void state_Logo() {

		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			if(State_Logo.lgo == 0 )
			{	
				logo.init();
				
				menu = new State_MainMenu(this);
				loading = new State_Loading(this);
				arena = new State_Select_Arena(this);
				SetNextMessageState(NextMessageState());
			}
			else
			{
				logo.init();
				play = new State_GamePlay(this);
				pause = new State_GamePause(this);
				SetNextMessageState(NextMessageState());
			}
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE) 
		{
			logo.update();
//			SetNextMessageState(NextMessageState());
			
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT) 
		{
			logo.exit();
			
		}
		
	}
	
	
	private void state_Loading() 
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			
			loading.init();
			SetNextMessageState(NextMessageState());
		}
		else if(messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			loading.update();
		}
		else
		{
			loading.exit();
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
	}
	
	private void state_Menu()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			menu.init();
			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE) 
		{
			menu.update();
//			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT) 
		{
			menu.exit();
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}

	private void state_Arena() 
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			
			arena.init();
			SetNextMessageState(NextMessageState());
		}
		else if(messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			arena.update();
		}
		else
		{
			arena.exit();
		}
		
	}
	
	private void state_GamePlay() {
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
		{
			play.init();
			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE) 
		{
			play.update();
//			SetNextMessageState(NextMessageState());
			
		} 
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT) 
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
	}
		
		private void state_Pause() {
			if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT) 
			{
				pause.init();
				SetNextMessageState(NextMessageState());
				
			} 
			else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE) 
			{
				pause.update();
//				SetNextMessageState(NextMessageState());
				
			} 
			else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT) 
			{
				pause.exit();
				SetNextMessageState(NextMessageState());
				SetNextGameState(iGameDefines.GAME_STATE_GAME_PLAY);
			}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		switch (gameCurrentState) {
		
		case GAME_STATE_MAIN_MENU:
			menu.back(keyCode, event);
			break;
		
		case GAME_STATE_SELECT_ARENA:
			arena.back(keyCode, event);
			break;
		
		case GAME_STATE_GAME_PLAY:
			play.back(keyCode, event);
			break;

		case GAME_STATE_PAUSE:
			pause.back(keyCode, event);
			break;
			
		}
		return super.onKeyDown(keyCode, event);
	}

	
	
	
	

}
